<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>入库单管理</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <script src="../../layui/layui.js"></script>
</head>
<body>
<div class="layui-container">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-radius: 0;border-top: 4px solid #d2d2d2;">
            <div class="layui-col-md12" align="right">
                <button class="layui-btn" id="printBtn">打印入库单</button>
            </div>
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-col-md12">
            <table class="layui-table" id="rukuTable" ></table>
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-col-md12" id="rukuTablePage"></div>
    </div>
</div>
<script>
    layui.use(['layer', 'jquery', 'laypage','form'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var laypage = layui.laypage;
        var form = layui.form;

        var tableHead = '<thead>' +
            '<tr>' +
            '<th>勾选</th>' +
            '<th>ID</th>' +
            '<th>入库单号</th>' +
            '<th>入库时间</th>' +
            '<th>药品总金额</th>' +
            '<th>负责人</th>' +
            '<th>备注</th>' +
            '</tr>' +
            '</thead>';


        let chukuData;
        let currentLimit = 10;

        // 分页请求数据
        function getData(page, limit) {
            if (limit !== currentLimit) {
                currentLimit = limit; // Update currentLimit value
            }
            $.ajax({
                url: '/print/ruku',
                type: 'GET',
                data: {
                    page: page,
                    limit: limit
                },
                success: function (res) {
                    if (res.code === 1) {
                        rukuData = res.data
                        var html = '';
                        for (var i = 0; i < rukuData.length; i++) {
                            var sales = rukuData[i];
                            var shijian = new Date(sales.orderTime);
                            var formattedDate = shijian.getFullYear() + '-' + (shijian.getMonth() + 1).toString().padStart(2, '0') + '-' + shijian.getDate().toString().padStart(2, '0');
                            html += '<tr>';
                            html += '<td style="width: 150px;"><input type="checkbox" class="selectItem"></td>';
                            html += '<td style="width: 150px;">' + sales.id + '</td>';
                            html += '<td style="width: 150px;">' + sales.orderNumber + '</td>';
                            html += '<td style="width: 200px;">' + formattedDate + '</td>';
                            html += '<td style="width: 150px;">' + sales.price + '</td>';
                            html += '<td style="width: 150px;">' + sales.leader + '</td>';
                            html += '<td style="width: 150px;">' + sales.remark + '</td>';
                            html += '</tr>';
                        }

                        $("#rukuTable").html(tableHead + html); //加载数据

                        // Render the pagination
                        laypage.render({
                            elem: 'rukuTablePage',
                            count: res.total, // Total number of data entries
                            curr: res.page, // Current page number
                            limit: currentLimit, // Use the currentLimit value instead of obj.limit
                            limits: [5, 10, 15, 20], // Display count options
                            layout: ['prev', 'page', 'next', 'limit', 'count', 'skip'],
                            jump: function (obj, first) {
                                if (!first) {
                                    // Call getData function with the updated page and limit values
                                    getData(obj.curr, obj.limit);
                                }
                            }
                        });
                    } else {
                        // 请求失败时提示错误信息
                        layer.msg(res.message, { icon: 2 });
                    }
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            });
        }

        // 初始化第一页数据
        getData(1, 10);

        // 打印按钮点击事件
        $('#printBtn').on('click', function () {
            var selectedData = []; // 存储选中的数据
            $(".selectItem:checked").each(function () {
                var row = $(this).closest("tr");
                var sales = {
                    danjuhao: 'Num',
                    danjuriqi: row.find("td:eq(1)").text(),
                    zongjiage: row.find("td:eq(2)").text(),
                    jingshouren: row.find("td:eq(3)").text(),
                    beizhu: row.find("td:eq(4)").text()
                };
                selectedData.push(sales);
            });

            if (selectedData.length > 0) {
                // 将选中的数据打印成CSV格式
                var csvContent = "data:text/csv;charset=utf-8-sig;";
                csvContent += "销售单据号,销售时间,药品总金额,负责人,备注\n";

                for (var i = 0; i < selectedData.length; i++) {
                    var sales = selectedData[i];
                    csvContent += sales.danjuhao + ",,"
                        + sales.danjuriqi + ",,"
                        + sales.zongjiage + ",,"
                        + sales.jingshouren + ",,"
                        + sales.beizhu + ",,\n";
                }

                // 创建一个隐藏的链接，并设置其href属性为CSV内容，以便下载
                var encodedUri = encodeURI(csvContent);
                var link = document.createElement("a");
                link.setAttribute("href", encodedUri);
                link.setAttribute("download", "selected_data.csv");
                document.body.appendChild(link);

                // 模拟点击链接进行下载
                link.click();

                // 清空复选框
                $(".selectItem:checked").prop("checked", false);
            } else {
                layer.msg('请选择要打印的数据', {icon: 2});
            }
        });

        // 监听表单提交事件
        form.on('submit(search)', function(data) {
            // Get the page and limit values from the form data
            var page = data.field.page;
            var limit = data.field.deptno;

            // Call getData function to perform the search
            getData(page, limit);

            // Prevent the default form submission behavior
            return false;
        });

    });
</script>
</body>
</html>